package com.algebra.activiti.web.base;


import com.algebra.activiti.domain.ActivitiVariable;
import com.algebra.activiti.domain.FormData;
import com.algebra.activiti.service.ActivitiService;
import com.algebra.activiti.service.ActivitiVariableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/activiti")
@Api(tags = "模型业务逻辑（测试）")
public class ActivitiProcessController {

    @Resource
    private ActivitiService activitiServiceImpl;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ActivitiVariableService activitiVariableService;


    @RequestMapping("/start/{key}")
    @ApiOperation("开启流程")
    public String startProcess(@PathVariable("key") String key, RedirectAttributesModelMap modelMap) {
//        Map<String,Object> map = new HashMap<String,Object>();
//        map.put("userId",userIdSl);
//        map.put("businessKey",business_key);
//        String business_key = key + ":" + key;
        ActivitiVariable activitiVariable = new ActivitiVariable();
        activitiVariable.setApplyStr(key);
        boolean resul = activitiVariableService.save(activitiVariable);
        if (resul) {
            modelMap.addFlashAttribute("info", "流程启动成功！");
            String businessKey = key + ":" + activitiVariable.getId();
            activitiServiceImpl.startProcesses(key, businessKey);
        } else {
            modelMap.addFlashAttribute("info", "流程启动失败！");
        }
        return "redirect:/models/modelList";
    }

    @RequestMapping("/queryTaskSl")
    @ApiOperation("获取受理员任务列表")
    public String findTasksForSL(ModelMap modelMap, FormData formData) {
        List<Task> lists = activitiServiceImpl.findTasksByUserId(formData.getUserId());
        modelMap.addAttribute("tasks", lists);
        modelMap.addAttribute("userId", formData.getUserId());
        return "model/taskList";
    }

    @RequestMapping("/form")
    public String form(FormData formData, ModelMap modelMap) {
        Task task = activitiServiceImpl.findTaskById(formData.getId());
        modelMap.addAttribute("data", formData);
        modelMap.addAttribute("task", task);
        if (StringUtils.isNotEmpty(task.getFormKey())) {
            return "activitiForm/" + task.getFormKey();
        }
        return "model/form";
    }


    //受理员受理数据
    @RequestMapping("/completeTaskSl")
    public String completeTasksForSL(ModelMap modelMap, FormData formData) {
        activitiServiceImpl.completeTask(formData.getId(), formData.getUserId(), formData.getAttr1());//受理后，任务列表数据减少
        return findTasksForSL(modelMap, formData);
    }

    //设置流程变量
    //设置流程变量【基本类型】
    public void setTasksVar() {
        List<Task> lists = activitiServiceImpl.findTasksByUserId("sly1");
        for (Task task : lists) {//不知为何，变量保存成功，但数据表只有请假天数含有任务id，单获取流程变量时，根据任务id均可获取到（如下一测试）
            taskService.setVariable(task.getId(), "请假人", "sly1");
            taskService.setVariableLocal(task.getId(), "请假天数", 3);
            taskService.setVariable(task.getId(), "请假日期", new Date());
        }
    }

}
